package com.example.myapplication10.Activities;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.myapplication10.Buttons.Btn_Add;
import com.example.myapplication10.Buttons.Btn_smallAQI;
import com.example.myapplication10.MsgEvents.MsgEventForecast;
import com.example.myapplication10.MsgEvents.MsgEventOver;
import com.example.myapplication10.MsgEvents.MsgEventTitle;
import com.example.myapplication10.MsgEvents.MsgEventUpdateOver;
import com.example.myapplication10.MsgEvents.MsgEventUpdateTime;
import com.example.myapplication10.R;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.hxc.basemodule.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class MainActivity extends BaseActivity{
    public List<Fragment> viewPageFragListMain;
    public ArrayList<TabLayout.Tab> tabList;
    boolean initFlag = false;boolean delFlag = false;
    boolean noResumeFlag = false;
    boolean resumeFlag = false;
    boolean noCitiesFlag = false;
    int year=-1; int day=-1;
    ViewPager2 weatherViewPages;

//    public int position = 0;
//    public int itemCount = 0;
//    public ArrayList<Map<String, String>> initCityList;

    TabLayout weatherTabLayout;
    MyAdapter myAdapter;
    TextView timeTextOfWeatherUpdateView;
//    String cityCode;String cityName;
//    TabLayout tabLayout;



//    public static String getWeatherJson(String url) throws IOException {
//        OkHttpClient client = new OkHttpClient();
//        Request request = new Request.Builder().url(url).build();
//        Response response = client.newCall(request).execute();
//        if (response.isSuccessful()) {
//            return response.body().string();
//        } else {
//            throw new IOException("Unexpected code " + response);
//        }
//    }

//    public void selectShownPages(int index, String str, weatherFrag1 weatherFrag01) {
////        View view = weatherFrag01.getView();
//        int shown;
//        switch (index) {
//            case 0:
//                shown = R.id.shown1;
//                break;
//            case 1:
//                shown = R.id.shown2;
//                break;
//            case 2:
//                shown = R.id.shown3;
//                break;
//            case 3:
//                shown = R.id.shown4;
//                break;
//            case 4:
//                shown = R.id.shown5;
//                break;
//            case 5:
//                shown = R.id.shown6;
//                break;
//            case 6:
//                shown = R.id.shown7;
//                break;
//            case 7:
//                shown = R.id.shown8;
//                break;
//            default:
//                shown = R.id.shown1;
//                break;
//        }
//        weatherFrag01.setShown(shown);
//        weatherFrag01.updateStr(str);
////        shown.setImageResource(R.mipmap.is_shown);
////        weatherFrag01.setShown(shown);
//    }

    //    private List<Fragment> selectShownPages(List<Fragment> fragments){
//        for(Fragment fragment:fragments) {
//            View view = inflater.inflate(R.layout.weather_fragment,null);
//            ImageView shown = view.findViewById(R.id.shown1);
//            int index = fragments.indexOf(fragment);
//            switch (index) {
//                case 0:
//                    shown = view.findViewById(R.id.shown1);
//                    break;
//                case 1:
//                    shown = view.findViewById(R.id.shown2);
//                    break;
//                case 2:
//                    shown = view.findViewById(R.id.shown3);
//                    break;
//                case 3:
//                    shown = view.findViewById(R.id.shown4);
//                    break;
//                case 4:
//                    shown = view.findViewById(R.id.shown5);
//                    break;
//                case 5:
//                    shown = view.findViewById(R.id.shown6);
//                    break;
//                case 6:
//                    shown = view.findViewById(R.id.shown7);
//                    break;
//                case 7:
//                    shown = view.findViewById(R.id.shown8);
//                    break;
//                default:
//                    break;
//            }
//            shown.setImageResource(R.mipmap.is_shown);
//        }
//        return fragments;
//    }




//
//        for (Map<String,String> map : list){
//            if (!addCityList.contains(map) && !cityList.contains(map)){
//                this.addCityList.add(map);
//            }
//        }




    public void initCityView() {
        viewPageFragListMain = new ArrayList<>();
        weatherTabLayout = findViewById(R.id.tab_layout);
        tabList = new ArrayList<>();
        timeTextOfWeatherUpdateView = findViewById(R.id.update_time);
        ArrayList<Map<String,String>> list = new ArrayList<>();
        Map<String,String> map0;
        SharedPreferences sharedPreferences
                = getSharedPreferences("search_history", MODE_PRIVATE);
        if(sharedPreferences.getAll().size()==0){
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("慈溪","101210403");
            editor.apply();
        }




        Map<String, ?> map = sharedPreferences.getAll();
        if (map.size() < 15 && map.size() != 0) {
            for (Map.Entry<String, ?> entry : map.entrySet()) {
                map0 = new HashMap<>();
                map0.put("城市名称", entry.getKey());
                map0.put("城市代码", entry.getValue().toString());
                list.add(map0);
            }
        }
        if (list.size()!=0) {
            showProgressDialog("极简天气","加载中......");
            myAdapter = new MyAdapter(this.getBaseContext(), weatherTabLayout,
                    tabList, list, viewPageFragListMain, getSupportFragmentManager(), getLifecycle());
        }
        else {
//            hideProgressDialog();
            Btn_Add btnAdd = findViewById(R.id.btn_add);
            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initFlag = true;
                    Intent intent = new Intent(MainActivity.this, AddActivity.class);
                    intent.putExtra("addedCities", "");
                    startActivityForResult(intent, 1);
                    //                    EventBus.getDefault().post
                    //                            (new MsgEventAddIntent(weatherFragmentAdapter.getCityString()));
                }
            });
        }
//        setInitCityList();
    }
//        viewPager2 = findViewById(R.id.weather_view0);
//        viewPager2.setAdapter(adapter);
//        new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
//            @Override
//            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
//                tab.setCustomView(R.layout.unselected);
//                tabs.add(position,tab);
//            }
//        }).attach();
//        weatherFragmentAdapter.setTabLayout(tabList);


//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void messageEventBusInit(MsgEventInit event) {
//        Weather weather = event.getMsg();
//        new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
//            @Override
//            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
//                tab.setCustomView(R.layout.unselected);
//                tabs.add(position,tab);
//            }
//        }).attach();
//        setTitle(adapter.getTitle());
//
//    }

//        tabLayout.setSelectedTabIndicator(R.mipmap.is_shown);
//        tabLayout.setSelectedTabIndicatorGravity(TabLayout.INDICATOR_GRAVITY_CENTER);
//        tabLayout.setSelectedTabIndicatorColor(0xFFFFFFFF);

//        viewPager2.addView(selectShownPages(fragment,2));




//    class MyThread2 implements Runnable {
//        @Override
//        public void run() {
//            String msg = null;
//            try {
//                msg = getWeatherJson("http://t.weather.sojson.com/api/weather/city/" + cityCode);
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//            EventBus.getDefault().post(new MsgEvent(msg));
//        }
//    }

//    class MyThread implements Runnable {
//        @Override
//        public void run() {
//            String msg = null;
//            Weather weather = new Weather();
//            int itemCount0 = cityList.toArray().length;
//            for (Map<String,String> map:cityList){
//                String cityCode = map.get("城市代码");
//                try {
//                    msg = getWeatherJson("http://t.weather.sojson.com/api/weather/city/" + cityCode);
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//                JSONObject object = JSONObject.parseObject(msg);
//                weather = object.toJavaObject(Weather.class);
//                if(cityList.indexOf(map) == 0) {
//                    EventBus.getDefault().post(new MsgEventInit(weather));
//                    itemCount = itemCount0;
//                }
//                else{EventBus.getDefault().post(new MsgEventWeather(weather));}
//            }
//        }
//    }
    public void resume(){
        ArrayList<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map0;
        SharedPreferences sharedPreferences
                = getSharedPreferences("search_history", MODE_PRIVATE);
        Map<String, ?> map = sharedPreferences.getAll();
        if (map.size() < 15 && map.size() != 0) {
            for (Map.Entry<String, ?> entry : map.entrySet()) {
                map0 = new HashMap<>();
                map0.put("城市名称", entry.getKey());
                map0.put("城市代码", entry.getValue().toString());
                list.add(map0);
            }
        }
        if (list.size() != 0) {
            showProgressDialog("极简天气", "恢复中......");
            myAdapter.setCityListFromHistory(list);
//                weatherFragmentAdapter = new MyAdapter(this.getBaseContext(), weatherTabLayout,
//                        tabList, list, viewPageFragListMain, getSupportFragmentManager(), getLifecycle());
        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        resumeFlag = true;

        int day0= LocalDate.now().getDayOfYear();
        int year0= LocalDate.now().getYear();

        if(!noResumeFlag && !initFlag && day != day0 || day==day0&&year!=year0) {
            resume();
//                weatherFragmentAdapter = new MyAdapter(this.getBaseContext(), weatherTabLayout,
//                        tabList, list, viewPageFragListMain, getSupportFragmentManager(), getLifecycle());
        } else if (!noResumeFlag && !initFlag && myAdapter.cityWeatherList.size() == 0) {
            resume();
        }
    }



    //        if (!initFlag && !noResumeFlag) {
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            ArrayList<Map<String, String>> list = new ArrayList<>();
//            Map<String, String> map0;
//            SharedPreferences sharedPreferences
//                    = getSharedPreferences("search_history", MODE_PRIVATE);
//            Map<String, ?> map = sharedPreferences.getAll();
//            if (map.size() < 15 && map.size() != 0) {
//                for (Map.Entry<String, ?> entry : map.entrySet()) {
//                    map0 = new HashMap<>();
//                    map0.put("城市名称", entry.getKey());
//                    map0.put("城市代码", entry.getValue().toString());
//                    list.add(map0);
//                }
//            }
//            weatherFragmentAdapter.setCityListFromHistory(list);
//        }
//        else {
//            initFlag = false;
//        }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        tabLayout = findViewById(R.id.tab_layout);
        setContentView(R.layout.activity_main);
        initFlag = true;
        EventBus.getDefault().register(this);
        setImmersiveWindows();
        setTitle("请添加城市");


        day= LocalDate.now().getDayOfYear();
        year= LocalDate.now().getYear();

        Btn_smallAQI about = findViewById(R.id.about);
        about.setText("关于我们");

        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAlertDialog("极简天气","\n版本号：1.1\n姓名：傅振轩\n学号：232821411",R.mipmap.app_launcher);
            }
        });
        if (initFlag){
            initCityView();
            noResumeFlag = true;
        }


//        while (adapter.isOver == false);

//        tv=findViewById(R.id.tv1);
//        Thread thread = new Thread(new MyThread());
//        thread.start();
    }

////        SharedPreferences sharedPreferences =
////                getSharedPreferences("weather", Activity.MODE_PRIVATE);
////        SharedPreferences.Editor editor = sharedPreferences.edit();
//        ArrayList<Map<String,String>> cityList = new ArrayList<>();
//        Map<String,String> map = new HashMap<>();
////        Set<String> setCityName = new HashSet<>();
////        Set<String> setCityCode = new HashSet<>();
//        map.put("城市名称","宁波");
//        map.put("城市代码","101210401");
//        cityList.add(map);
////        setCityName.add(map.get("城市名称"));
////        setCityCode.add(map.get("城市代码"));
//        map = new HashMap<>();
//        map.put("城市名称","金华");
//        map.put("城市代码","101210901");
////        setCityName.add(map.get("城市名称"));
////        setCityCode.add(map.get("城市代码"));
//        cityList.add(map);
////        editor.putStringSet("城市名称",setCityName);
////        editor.putStringSet("城市代码",setCityCode);
////        editor.apply();


    //    @Subscribe(threadMode = ThreadMode.MAIN)
    @Override
    protected int setCurrentLayoutId() {
        return R.layout.activity_main;
    }
//    public void MoveToPosition(int n) {
//        LinearLayoutManager manager0 = this.manager;
//        manager0.scrollToPositionWithOffset(n, 0);
//        manager0.setStackFromEnd(true);
//    }



    class ForecastThread implements Runnable {
        @Override
        public void run() {
            try {
                Intent intent = new Intent(MainActivity.this, ForecastActivity.class);
                startActivity(intent);
                noResumeFlag = true;
                Thread.sleep(100);//必须！




                EventBus.getDefault().post(new MsgEventForecast
                        (myAdapter.cityWeatherList.get(myAdapter.tabPosition)));
                this.finalize();
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void messageEventBusTitle(MsgEventTitle event) throws InterruptedException {
        if(Objects.equals(event.getMsg(), "近15日天气")){
            Thread thread = new Thread(new ForecastThread());
            thread.start();
        }
        else {
            setTitle(event.getMsg());


        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void messageEventBusUpdateTimeView(MsgEventUpdateTime event) {
        timeTextOfWeatherUpdateView.setText(event.getMsg());
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void messageEventBusUpdateOverView(MsgEventUpdateOver event) {
        timeTextOfWeatherUpdateView.setText(event.getMsg());
        hideProgressDialog();
    }
//        String str = event.getMsg();
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
//        int offset = LocalTime.parse(str,formatter).compareTo(LocalTime.now());
//        if (offset >= -10 && offset <= 0){
//            hideProgressDialog();
//        }
//    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void messageEventBusOver(MsgEventOver event) throws InterruptedException {
        String s = event.getMsg();
        if (myAdapter.failFlag|| Objects.equals(s, "接口异常")){
            hideProgressDialog();
            myAdapter.initThread.interrupt();
            showAlertDialog("接口调用失败！", event.getMsg(), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "正在退出程序......", Toast.LENGTH_SHORT).show();
                    finish();
                }
            });
        }
        else {
            weatherViewPages = findViewById(R.id.weather_view0);
            weatherViewPages.setAdapter(myAdapter);
            tabList.clear();
            new TabLayoutMediator(weatherTabLayout, weatherViewPages, new TabLayoutMediator.TabConfigurationStrategy() {
                @Override
                public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                    tab.setCustomView(R.layout.unselected);
                    tabList.add(tab);
                }
            }).attach();
            myAdapter.setTabLayout(tabList);
            setTitle(event.getMsg());
            if (Objects.equals(s, "添加")||Objects.equals(s, "删除")) {
                Toast.makeText(this, "城市已修改", Toast.LENGTH_SHORT).show();

                //                tabList.get(0).select();
            }
//            else if(Objects.equals(s, "添加")) {
//                tabList.get(weatherFragmentAdapter.tabPosition).select();
//            }
            else if (initFlag && noResumeFlag || resumeFlag) {
                tabList.get(0).select();
                //        weatherViewPages.setOffscreenPageLimit(1);
                //        viewPager2.setOffscreenPageLimit(5);
                Btn_Add btnAdd = findViewById(R.id.btn_add);
                btnAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this, AddActivity.class);
                        intent.putExtra("addedCities", myAdapter.getCityString());
                        startActivityForResult(intent, 1);
                        //                    EventBus.getDefault().post
                        //                            (new MsgEventAddIntent(weatherFragmentAdapter.getCityString()));
                    }
                });

//                Thread.sleep(1000);

                timeTextOfWeatherUpdateView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = weatherTabLayout.getSelectedTabPosition();
                        myAdapter.setUpdatePosition(pos);
                        myAdapter.updateFlag = true;
                        showProgressDialog("极简天气", "更新中......");
                    }
                });
                setTitle(myAdapter.cityList.get(0).get("城市名称"));
                timeTextOfWeatherUpdateView.setText(myAdapter.cityWeatherList.
                        get(0).getCityInfo().getUpdateTime());
                hideProgressDialog();
            }
            else if(myAdapter.addFlag){
                myAdapter.addFlag =false;




            }
//            else {
//
//            }

        }

        initFlag = false;
        resumeFlag = false;
        noResumeFlag = false;


    }

    public void setResumeCityList(){
        ArrayList<Map<String,String>> list = new ArrayList<>();
        Map<String,String> map0;
        SharedPreferences sharedPreferences
                = getSharedPreferences("search_history", MODE_PRIVATE);
        Map<String, ?> map = sharedPreferences.getAll();
        if (map.size() < 15 && map.size() != 0) {
            for (Map.Entry<String, ?> entry : map.entrySet()) {
                map0 = new HashMap<>();
                map0.put("城市名称", entry.getKey());
                map0.put("城市代码", entry.getValue().toString());
                list.add(map0);
            }
        }

        if (list.size()!=0) {
            myAdapter.setCityListFromHistory(list);
//            weatherFragmentA
//            showProgressDialog("极简天气","添加中......");
//            weatherFragmentAdapter = new MyAdapter(this.getBaseContext(), weatherTabLayout,
//                    tabList, list, viewPageFragListMain, getSupportFragmentManager(), getLifecycle());

        }
        else {
//            hideProgressDialog();
            Btn_Add btnAdd = findViewById(R.id.btn_add);
            initFlag = true;
            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, AddActivity.class);
                    intent.putExtra("addedCities", "reset");
                    startActivityForResult(intent, 1);
                    //                    EventBus.getDefault().post
                    //                            (new MsgEventAddIntent(weatherFragmentAdapter.getCityString()));
                }
            });
//hideProgressDialog();
        }

    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void setResumeCityList(MsgEventResumeCity event) throws InterruptedException {
//        ArrayList<Map<String,String>> list = (ArrayList<Map<String, String>>) event.getMsg();
//
//
//
//
//
//        if (list.size()!=0) {
//            weatherFragmentAdapter.setCityListFromHistory(list);
//            //            Thread thread = new Thread(new DelayThread());thread.start();
////            weatherFragmentA
//
//        }
//        else {
////            hideProgressDialog();
//            Btn_Add btnAdd = findViewById(R.id.btn_add);
//            initFlag = true;
//            btnAdd.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent(MainActivity.this, AddActivity.class);
//                    intent.putExtra("addedCities", "reset");
//                    startActivityForResult(intent, 1);
//                    //                    EventBus.getDefault().post
//                    //                            (new MsgEventAddIntent(weatherFragmentAdapter.getCityString()));
//                }
//            });
//
//        }
//
//    }
//
//
    public void noCitiesMode(){

        setTitle("请添加城市");
        timeTextOfWeatherUpdateView.setVisibility(View.GONE);
        weatherTabLayout.removeAllTabs();
        weatherViewPages.setVisibility(View.INVISIBLE);
        Btn_Add btnAdd = findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddActivity.class);
                intent.putExtra("addedCities", "reset");
                startActivityForResult(intent, 1);
                weatherViewPages.setVisibility(View.VISIBLE);
                timeTextOfWeatherUpdateView.setVisibility(View.VISIBLE);
                //                    EventBus.getDefault().post
                //                            (new MsgEventAddIntent(weatherFragmentAdapter.getCityString()));
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, null);
        switch (resultCode){
            case 1://A
//                int pos = weatherTabLayout.getSelectedTabPosition();
//                weatherFragmentAdapter.setUpdatePosition(pos);
                noResumeFlag = true;
                //                weatherFragmentAdapter.addFlag = true;
//                weatherFragmentAdapter.addExecute = true;
 //               showProgressDialog("极简天气","添加中......");
                EventBus.getDefault().post(new MsgEventOver("添加"));
                break;

            case 0: //D
                delFlag = true;
                noResumeFlag = true;
                if (myAdapter.cityList.size() != 0){
                    EventBus.getDefault().post(new MsgEventOver("删除"));
                }
                break;
            case -1://RESTART
                finish();
                break;

            case -2:
                noResumeFlag = true;
                Toast.makeText(this, "提示：未添加/删除城市", Toast.LENGTH_SHORT).show();
                break;



            default:
//                showAlertDialog("错误","未找到城市");

                break;

        }
    }


    class DelayThread implements Runnable {
        @Override
        public void run() {
            try {
                while (myAdapter.addExecute) {
                    Thread.sleep(100);
                }
                hideProgressDialog();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }


//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void messageEventBus(MsgEventWeather event) {
//        Weather weather = event.getMsg();
//        cityWeather.add(weather);
//        this.position += 1;
//        adapter.setWeather(weather);
//        adapter.createFragment(this.position);
//    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        myAdapter.initThread.interrupt();
        myAdapter.updateThread.interrupt();
        myAdapter.addThread.interrupt();
        EventBus.getDefault().unregister(this);
    }
}

//    List datas = new ArrayList();
//    MyAdapter adapter;

//
////    TextView tv;
//
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
////        tv=findViewById(R.id.tv1);
//        EventBus.getDefault().register(this);
//        Thread thread = new Thread(new MyThread());
//        thread.start();
//    }
//


//    static class MyThread implements Runnable {
//        @Override
//        public void run() {
//            String msg = null;
//            try {
//                msg = getWeatherJson("http://t.weather.sojson.com/api/weather/city/101210401");
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//            EventBus.getDefault().post(new MsgEvent(msg));
//        }
//    }

///////////    @Subscribe(threadMode = ThreadMode.MAIN)

//    public void messageEventBus(MsgEvent event) {
//        String str = event.getMsg();
//        Weather weather = new Weather();
//        CityInfoDTO cityInfo = new CityInfoDTO();
//        DataDTO data = new DataDTO();
//        YesterdayDTO yesterday = new YesterdayDTO();
//        List<ForecastDTO> forecastArrayList = new ArrayList<>();
//        JSONObject obj = JSONObject.parseObject(str);
//        int status = obj.getInteger("status");
//        if(status == 200){
//            weather = obj.toJavaObject(Weather.class);
////            RecyclerView recyclerView = findViewById(R.id.weather_view);
////            LinearLayoutManager manager = new LinearLayoutManager(this);
////            recyclerView.setLayoutManager(manager);
////            adapter = new MyAdapter(weather);
////            recyclerView.setAdapter(adapter);
////            recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL));
//        }






//        JSONObject data = obj.getJSONObject("data");
//        JSONArray forecast = data.getJSONArray("forecast");
//        String currentTempStr = data.get("wendu").toString();
//        List listout = new ArrayList();
////        Map map = new HashMap<>();
////        int startDate = Integer.parseInt(forecast.getJSONObject(0).getString("date"));
//        for(int i = 0;i < 15;i++)
//        {
//            String out = "";
//            String ymdDate = forecast.getJSONObject(i).getString("ymd");
//            String dayOfWeek = forecast.getJSONObject(i).getString("week");
//            String high = forecast.getJSONObject(i).getString("high");
//            String low = forecast.getJSONObject(i).getString("low");
//            String weather0 = forecast.getJSONObject(i).getString("type");
//            String windLevel = forecast.getJSONObject(i).getString("fl");
//            String windDirection = forecast.getJSONObject(i).getString("fx");
//            String notice = forecast.getJSONObject(i).getString("notice");
//            low = low.replace("低温 ","");
//            high = high.replace("高温 ","");
//            if(i == 0) {
//                out = String.format("日期：%s %s\n（实时：%s℃） 天气：%s\n风力：%s 风向：%s 温度：%s ~ %s\n备注：%s\n"
//                        , ymdDate, dayOfWeek, currentTempStr, weather, windLevel, windDirection, low, high, notice);
//                listout.add(out);
//            }
//            else{
//                out = String.format("日期：%s %s\n天气：%s 风力：%s 风向：%s 温度：%s ~ %s\n备注：%s\n"
//                        , ymdDate, dayOfWeek, weather, windLevel, windDirection, low, high, notice);
//                listout.add(out);
//            }
//        }
//        tv.setText(strout);
//        this.datas = listout;

//    }
